// 上面这个代码处理过度动画（默认加上不用管）
document.addEventListener('DOMContentLoaded', () => {
  setTimeout(() => {
    document.body.classList.add('sidenav-pinned')
    document.body.classList.add('ready')
  }, 200)
})
function tip(msg) {
  const myToast = document.querySelector('#myToast')
  const toast = new bootstrap.Toast(myToast, {
    delay : 1000
  })
  myToast.querySelector('.toast-body').innerHTML = msg
  toast.show()
}

axios.defaults.baseURL = 'http://ajax-api.itheima.net'

const uname = document.querySelector('.navbar-nav .font-weight-bold')
uname && (uname.innerHTML = localStorage.getItem('user-name'))

const logout = document.querySelector('#logout')
logout && logout.addEventListener('click',function(){
  localStorage.removeItem('user-token')
  localStorage.removeItem('user-name')
  location.href = 'login.html'
})

axios.interceptors.request.use(function (config) {
  console.log(config);
  // 在发送请求之前做些什么
  localStorage.getItem('user-token') && (config.headers.authorization = localStorage.getItem('user-token'))
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  return response.data;
}, function (error) {
  // 对响应错误做点什么
  if(error.response.status === 401){
    alert('token失效')
    return location.href = 'login.html'
  }
  return Promise.reject(error);
});